package com.wolfcode.category.service.impl;

import java.util.List;

import com.wolfcode.category.domain.GoodsStoreInfoVo;
import com.wolfcode.category.domain.vo.GoodsCategoryVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.wolfcode.category.mapper.GoodsCategoryMapper;
import com.wolfcode.category.domain.GoodsCategory;
import com.wolfcode.category.service.IGoodsCategoryService;
import org.springframework.util.Assert;
import org.springframework.util.StringUtils;

/**
 * 物品分类信息Service业务层处理
 *
 * @author wolfcode
 * @date 2022-12-10
 */
@Service
public class GoodsCategoryServiceImpl implements IGoodsCategoryService {
    @Autowired
    private GoodsCategoryMapper goodsCategoryMapper;

    /**
     * 查询物品分类信息
     *
     * @param id 物品分类信息主键
     * @return 物品分类信息
     */
    @Override
    public GoodsCategory selectGoodsCategoryById(Long id)
    {
        return goodsCategoryMapper.selectGoodsCategoryById(id);
    }

    /**
     * 查询物品分类信息列表
     *
     * @param goodsCategory 物品分类信息
     * @return 物品分类信息
     */
    @Override
    public List<GoodsCategory> selectGoodsCategoryList(GoodsCategory goodsCategory)
    {
        return goodsCategoryMapper.selectGoodsCategoryList(goodsCategory);
    }

    /**
     * 新增物品分类信息
     *
     * @param goodsCategoryVo 物品分类信息
     * @return 结果
     */
    @Override
    public int insertGoodsCategory(GoodsCategoryVo goodsCategoryVo) {
        Assert.notNull(goodsCategoryVo, "非法参数");
        Assert.notNull(goodsCategoryVo.getCategoryName(), "分类名称不能为空");
        Assert.state(goodsCategoryVo.getCategoryName().length() < 60,
                "分类名称不能超过30字");
        if (StringUtils.hasLength(goodsCategoryVo.getCategoryDesc())) {
            Assert.state(goodsCategoryVo.getCategoryDesc().length() < 400,
                    "描述不超过200字");
        }
        String parentId = goodsCategoryVo.getParentId();
        StringBuilder sb = new StringBuilder();
        int length = Integer.parseInt(parentId);
        // 拼接busi_id
        for (int i = 1; i <= length; i++) {
            sb = (i == length)
                    ? sb.append(i).append(":").append(i + 1)
                    : sb.append(i).append(":");

        }
        String path = sb.toString();
        goodsCategoryVo.setBusiPath(path);
        GoodsCategory goodsCategory = new GoodsCategory();
        BeanUtils.copyProperties(goodsCategoryVo, goodsCategory);
        return goodsCategoryMapper.insertGoodsCategory(goodsCategory);
    }

    /**
     * 修改物品分类信息
     *
     * @param goodsCategoryVo 物品分类信息
     * @return 结果
     */
    @Override
    public int updateGoodsCategory(GoodsCategoryVo goodsCategoryVo) {
        Assert.notNull(goodsCategoryVo, "非法参数");
        Assert.notNull(goodsCategoryVo.getCategoryName(), "分类名称不能为空");
        Assert.state(goodsCategoryVo.getCategoryName().length() < 60,
                "分类名称不能超过30字");
        if (StringUtils.hasLength(goodsCategoryVo.getCategoryDesc())) {
            Assert.state(goodsCategoryVo.getCategoryDesc().length() < 400,
                    "描述不超过200字");
        }
        GoodsCategory goodsCategory = new GoodsCategory();
        BeanUtils.copyProperties(goodsCategoryVo, goodsCategory);
        return goodsCategoryMapper.updateGoodsCategory(goodsCategory);
    }

    /**
     * 批量删除物品分类信息
     *
     * @param ids 需要删除的物品分类信息主键
     * @return 结果
     */
    @Override
    public int deleteGoodsCategoryByIds(Long[] ids) {
        return goodsCategoryMapper.deleteGoodsCategoryByIds(ids);
    }

    /**
     * 删除物品分类信息信息
     *
     * @param id 物品分类信息主键
     * @return 结果
     */
    @Override
    public void deleteGoodsCategoryById(Long id) {
        Assert.notNull(id, "非法参数");
        // 1.删除分类时连同子分类一起删除
        // 查询子分类
        GoodsCategory goodsCategory = goodsCategoryMapper.selectGoodsCategoryById(id);

        List<GoodsCategory> goodsCategories = goodsCategoryMapper
                .selectgoodsCategoryByBusiPath(goodsCategory.getBusiPath());
        System.out.println(goodsCategories.size());
        for (GoodsCategory category : goodsCategories) {
            // 获取物品库存
            List<GoodsStoreInfoVo> goodsStoreInfoVos = goodsCategoryMapper
                    .selectgoodsStoreByCategoryId(category.getId());
            if (goodsStoreInfoVos.isEmpty()) {
                goodsCategoryMapper.deleteGoodsCategoryById(category.getId());
                return;
            }
            for (GoodsStoreInfoVo goodsStoreInfoVo : goodsStoreInfoVos) {
                //2.删除分类时如当前分类连同子分类下的商品还有库存则不可以删除
                Assert.state(goodsStoreInfoVo.getAmounts() == 0,
                        "当前分类连同子分类下的商品还有库存则不可以删除");
                // 删除分类
                goodsCategoryMapper.deleteGoodsCategoryById(category.getId());

            }
        }


    }
}
